(function($, doc) {
	$.init({});		
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    
    window.onload = function(){ 
    	refresh_data();

		$.plusReady(function() {
			//关闭等待框
			plus.nativeUI.closeWaiting();
			//显示当前页面
			mui.currentWebview.show();		

			doc.getElementById('add_address').addEventListener('tap', function() {
				//打开新建地址页
				mui.openWindow({
					id: 'add_address.html',
					url: 'add_address.html',
					extras: {
						addr_id: 0//自定义扩展参数，可以用来处理页面间传值
					},
					show: {
						autoShow: false
					},
					waiting: {
						autoShow: true,
						title: '正在加载...'
					}
				});
			})

			mui("#main-area").on('tap',' .mui-table-view-cell .mui-checkbox input[name="checkbox"]',function(){
				var address_id=this.getAttribute('data-id');
				var username=this.getAttribute('data-name');
				var addrdesc=this.getAttribute('data-desc');
				var address=this.getAttribute('data-address');
				var mobile=this.getAttribute('data-mobile');
				mui.fire(plus.webview.getWebviewById('order.html'), 'refresh_address',{
						'address_id':address_id,
						'username':username,
						'addrdesc':addrdesc,
						'address':address,
						'mobile':mobile
				});
				mui.back();
			})

			window.addEventListener('refresh_data',function(event){
			    refresh_data();
			});
			
		});
		
	}
	
	
	function  refresh_data(){
		mui.ajax({
		    url: apiurl + "User/address_list/",
			data: {
				'user_id': uid,
				'_token':userinfo.token
			},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				if(res.status ==-3){
					mui.toast(res.msg);
					app.out();
					return false;
				}
				
				if(res.status==0) {
					mui.toast(res.msg);
					return false;
				} 
				// 请求成功
				doc.querySelector("#main-area").innerHTML = template('main-template', {
					'data': res.data
				});
		
			},
			error: function(xhr, type, errorThrown) {
				//console.log(JSON.stringify(xhr));
				//console.log(JSON.stringify(type));
				//console.log(JSON.stringify(errorThrown));
				mui.toast('提交失败！');
			}
		});
	}
		

}(mui, document));